package org.jpass.dc.common.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 * 包装器(wrapper)类HibernateUtil,
 * 静态初始化代码块中启动Hibernate
 * @author Administrator
 *
 */
public class HibernateUtil {
	private static final SessionFactory sessionFactory;

    static {
        try {

            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log exception!
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession()
            throws HibernateException {
        return sessionFactory.openSession();
    }
    
    public static void close(){
		if(sessionFactory !=null){
			sessionFactory.close();
		}
	}
}
